package com.ruoyi.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.domain.bo.EduTeacherBo;
import com.ruoyi.edu.domain.vo.EduTeacherVo;
import com.ruoyi.edu.domain.EduTeacher;
import com.ruoyi.edu.mapper.EduTeacherMapper;
import com.ruoyi.edu.service.IEduTeacherService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 讲师管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-18
 */
@RequiredArgsConstructor
@Service
public class EduTeacherServiceImpl implements IEduTeacherService {

    private final EduTeacherMapper baseMapper;
    private final SysOssMapper sysOssMapper;

    /**
     * 查询讲师管理
     */
    @Override
    public EduTeacherVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询讲师管理列表
     */
    @Override
    public TableDataInfo<EduTeacherVo> queryPageList(EduTeacherBo bo, PageQuery pageQuery) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        if (StringUtils.isNotEmpty(bo.getName())){
            wrapper.like("name",bo.getName());
        }
        if (StringUtils.isNotEmpty(bo.getIntro())){
            wrapper.like("intro",bo.getIntro());
        }
        if (StringUtils.isNotEmpty(bo.getCareer())){
            wrapper.like("career",bo.getCareer());
        }
        if (StringUtils.isNotEmpty(bo.getLevel())){
            wrapper.eq("level",bo.getLevel());
        }

        Page<EduTeacherVo> result = baseMapper.selectVoPage(pageQuery.build(), wrapper);
        List<EduTeacherVo> records = result.getRecords();
        ArrayList<EduTeacherVo> list = new ArrayList<>();
        for (EduTeacherVo eduTeacherVo:records){
            SysOssVo sysOssVo = sysOssMapper.selectVoById(eduTeacherVo.getAvatar());
            eduTeacherVo.setUrl(sysOssVo.getUrl());
            list.add(eduTeacherVo);
        }
        return TableDataInfo.build(list, result.getTotal());
    }

    /**
     * 查询讲师管理列表
     */
    @Override
    public List<EduTeacherVo> queryList(EduTeacherBo bo) {
        LambdaQueryWrapper<EduTeacher> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EduTeacher> buildQueryWrapper(EduTeacherBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EduTeacher> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), EduTeacher::getName, bo.getName());
//        lqw.like(StringUtils.isNotBlank(bo.getIntro()), EduTeacher::getIntro, bo.getIntro());
        lqw.like(StringUtils.isNotBlank(bo.getIntro()),EduTeacher::getIntro,bo.getIntro());
//        lqw.like(StringUtils.isNotBlank(bo.getCareer()), EduTeacher::getCareer, bo.getCareer());
        lqw.like(StringUtils.isNotBlank(bo.getCareer()),EduTeacher::getCareer,bo.getCareer());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), EduTeacher::getLevel, bo.getLevel());
        return lqw;
    }

    /**
     * 新增讲师管理
     */
    @Override
    public Boolean insertByBo(EduTeacherBo bo) {
        EduTeacher add = BeanUtil.toBean(bo, EduTeacher.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改讲师管理
     */
    @Override
    public Boolean updateByBo(EduTeacherBo bo) {
        EduTeacher update = BeanUtil.toBean(bo, EduTeacher.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EduTeacher entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除讲师管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R query() {
        List<EduTeacherVo> eduTeacherVos = baseMapper.selectVoList(null);
        return R.ok(eduTeacherVos);
    }
}
